// pages/detail/detail.js
const util = require('../../utils/util.js')
const api = require('../../utils/api.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: {},
    pid: 0,
    swiperCurrent: 0,
    show_gg: false,
    ggchoose: '请选择规格数量',
    ggchoosed: '',
    buyNumber: 1,
    buyNumMin: 1,
    buyNumMax: 0,
    colorChildIds: '',
    colorChildNames: '',
    selectedColor: '',
    selectedSize: '',
    sizeChildNames: '',
    sizeActive: 0,
    favicon: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    if (options.pid) {
      that.setData({
        pid: options.pid
      })
      that.getDetail();
    } else {
      util.showErrorToast("请输入商品ID")
      wx.navigateBack({})
    }
    if (options.show_gg) {
      that.setData({
        show_gg: options.show_gg
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (app.globalData.gohomeTop) {
      this.setData({
        top: app.globalData.gohomeTop
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '娜美达-' + this.data.product.title,
      path: '/pages/detail/detail?pid=' + this.data.product.id,
    }
  },
  setTouchMove: function(e) {
    var that = this;
    if (e.touches[0].clientY < 500 && e.touches[0].clientY > 0) {
      that.setData({
        top: e.touches[0].clientY
      })
      app.globalData.gohomeTop = e.touches[0].clientY
    }
  },



  // 获取详情
  getDetail: function() {
    wx.showToast({
      title: '加载中..',
      icon: 'none',
    })
    let that = this
    let url = api.ProductDetailUrl + that.data.pid
    util.request(url).then(function(res) {
      wx.hideToast()
      if (res.status == 'success') {
        that.setData({
          product: res.data.product,
          buyNumMax: res.data.product.stock,
          buyNumber: res.data.product.stock > 0 ? 1 : 0,
        })
        wx.hideLoading()
      } else {
        if (typeof(res.data) == 'string') {
          util.showErrorToast(res.data);
        } else {
          util.showErrorToast("错误，请重试");
        }
      }
    })
  },
  swiperChange: function swiperChange(e) {
    this.setData({
      swiperCurrent: e.detail.current
    });
  },

  // 打开规格dialog
  openPopup: function openPopup(e) {
    this.setData({
      show_gg: true
    });
  },
  // 点击遮罩关闭dialog
  toggleDialog() {
    this.setData({
      show_gg: !this.data.show_gg
    });
  },
  //规格选择弹窗
  labelItemTap: function labelItemTap(e) {
    var that = this;
    if (e.currentTarget.dataset.propertyindex == 1) {
      for (var i = 0; i < that.data.product.colors.length; i++) {
        that.data.product.colors[i].active = 0;
        if (that.data.product.colors[i].id == e.currentTarget.dataset.propertychildid) {
          that.data.product.colors[i].active = 1;
        }
      }
      var selectedColor = "";
      var colorChildIds = "";
      var colorChildNames = "";
      for (var j = 0; j < that.data.product.colors.length; j++) {
        if (that.data.product.colors[j].active) {
          colorChildIds = colorChildIds + that.data.product.colors[j].id + ",";
          colorChildNames = colorChildNames + that.data.product.colors[j].name + "  ";
          selectedColor = that.data.product.colors[j].name
        }
      }
      that.setData({
        product: that.data.product,
        colorChildIds: colorChildIds,
        selectedColor: selectedColor,
        colorChildNames: " 颜色:" + colorChildNames,
      });
    }
    // 尺码
    if (e.currentTarget.dataset.propertyindex == 2) {
      var sizeChildNames = "";
      for (var j = 0; j < that.data.product.size.length; j++) {
        if (that.data.product.size[j] == e.currentTarget.dataset.propertychildname) {
          sizeChildNames = that.data.product.size[j];
        }
      }
      that.setData({
        product: that.data.product,
        sizeActive: e.currentTarget.dataset.propertychildname,
        selectedSize: sizeChildNames,
        sizeChildNames: "  尺码:" + sizeChildNames,
      });
    }

  },
  //数量减少
  numJianTap: function numJianTap() {
    if (this.data.buyNumber > this.data.buyNumMin) {
      var currentNum = this.data.buyNumber;
      currentNum--;
      this.setData({
        buyNumber: currentNum
      });
    }
  },
  // 数量增加
  numJiaTap: function numJiaTap() {
    if (this.data.buyNumber < this.data.buyNumMax) {
      var currentNum = this.data.buyNumber;
      currentNum++;
      this.setData({
        buyNumber: currentNum
      });
    }
  },
  addToCart: function() {
    wx.showToast({
      title: '添加购物车...',
      icon: 'none',
    })
    let that = this
    if (that.data.selectedColor && that.data.selectedSize) {
      let url = api.AddCartItemsUrl
      util.requestByToken(url, {
        product_id: that.data.product.id,
        color: that.data.selectedColor,
        size: that.data.selectedSize,
        amount: that.data.buyNumber,
      }, 'POST').then(function(res) {
        if (res.status == 'success') {
          wx.showToast({
            title: '添加成功',
            icon: 'none',
          })
          wx.hideLoading()
        } else {
          if (typeof(res.data) == 'string') {
            util.showErrorToast(res.data);
          } else {
            util.showErrorToast("错误，请重试");
          }
        }
      })
    } else {
      util.showErrorToast("选择规格");
    }
  },
  //到购物袋
  goShoppingBag: function() {
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  // 弹窗购买按钮
  goBuy: function() {
    let that = this
    if (that.data.selectedColor && that.data.selectedSize) {
      app.checkLogin().then(res => {
        wx.navigateTo({
          url: '/pages/orderConfirm/orderConfirm?color=' + that.data.selectedColor + "&size=" + that.data.selectedSize + '&amount=' + that.data.buyNumber
        })
        //goto购买页面
      }).catch(res => {
        util.showErrorToast("登录失败");
      })
    } else {
      util.showErrorToast("选择规格");
    }
  },
  goback:function(){
    wx.navigateBack({
      
    })
  }

})